package com.kingtone.rongji.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.jakewharton.salvage.RecyclingPagerAdapter;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.HomeImage;
import com.kingtone.rongji.tool.ListUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {
	private static final String TAG = "ImagePagerAdapter";
	
    private Context       context;
    private List<HomeImage> imageIdList;

    private int           size;
    private boolean       isInfiniteLoop;
    
    private DisplayImageOptions mOptions;

    private OnImagePageClick mImagerPageClick = null;
    
    public interface OnImagePageClick {
    	public void onImagePageClick(int index);
    }
    
    public ImagePagerAdapter(Context context, List<HomeImage> imageIdList) {
        this.context = context;
        this.imageIdList = imageIdList;
        this.size = ListUtils.getSize(imageIdList);
        isInfiniteLoop = false;
		mOptions = new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565)
				.delayBeforeLoading(100).build();
    }
    
    public void setOnImagePageClickListener(OnImagePageClick listener) {
    	mImagerPageClick = listener;
    }

    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(imageIdList);
    }

    /**
     * get really position
     * 
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % size : position;
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            holder.imageView.setScaleType(ScaleType.FIT_XY);
            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        }
        
        final int index = getPosition(position);
        final HomeImage item = imageIdList.get(index);
        if(TextUtils.isEmpty(item.getImageUrl())) {
        	holder.imageView.setImageResource(item.getDefaultImage());
        } else {
        	ImageLoader.getInstance().displayImage(item.getImageUrl(), holder.imageView, mOptions);
        }
        
        holder.imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.d(TAG, "Click scroller image " + index);
				if(null!=mImagerPageClick) {
					mImagerPageClick.onImagePageClick(index);
				}
			}
		});
        return view;
    }

    private static class ViewHolder {

        ImageView imageView;
    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop the isInfiniteLoop to set
     */
    public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
    
    
}
